<!--
 * @Description: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2022-01-11 09:10:23
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2022-01-11 09:27:38
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>交叉链表</title>
</head>

<body>
    <script>
        // 获取链表长度

        // 思路是右对齐后做差，直到有相等元素就终止遍历
        var getLen = function (head) {
            let len = 0,
                cur = head;
            while (cur) {
                len++;
                cur = cur.next;
            }

            return len;
        }
        var getIntersectionNode = function (headA, headB) {
            let curA = headA, curB = headB,
                lenA = getListLen(headA),
                lenB = getListLen(headB);
            if (lenA < lenB) {
                [curA, curB] = [curB, curA];
                [lenA, lenB] = [lenB, lenA];
            }
            let i = lenA - lenB;
            while (i-- > 0) {
                curA = curA.next;
            }
            while (curA && curA != curB) {
                curA = curA.next;
                curB = curB.next;
            }

            return curA;




        }
    </script>
</body>

</html>